<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\ClockIn;
use App\Models\PersonnelDynamics;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class ClockInController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];


        if ( $request->ci_id) {
            $clock_in = ClockIn::findOrFail($request->ci_id);
            $clock_in->mtime = time();
        } else {
            $clock_in = new ClockIn();
            $clock_in->ctime = time();
        }
        $clock_in->ci_type = $request->ci_type;
        $clock_in->ci_place = $request->ci_place;
        $clock_in->ci_address = $request->ci_address;
        $clock_in->ci_scope = $request->ci_scope;
        $clock_in->company_id = $company_id;
        $clock_in->wifi_name = $request->wifi_name;
        $clock_in->wifi_mac = $request->wifi_mac;
        $clock_in->longitude = $request->longitude;
        $clock_in->latitude = $request->latitude;
        $clock_in->uid = $uid;

        $clock_in->save();

        return showError(Error::ERR_OK,['ci_id'=>$clock_in->ci_id]);

    }

    public function show(Request $request) {
        $clock_in = ClockIn::findOrFail($request->ci_id);
        return showError(Error::ERR_OK,['clock_in'=>$clock_in]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
        $name = $request->input('name');
        if (!empty($name))
        {
            $where['name'] = $name;
        }

        $where['company_id'] = $company_id;
        $where['status'] = 1;

        $list = ClockIn::getList($where);

        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        ClockIn::where('ci_id',$request->ci_id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }


}
